import os
import time
from mediadart.lock import locking, LOCK_EX, LOCK_NB, LockException


class Prova:
    tempfile = 'out.temp'
    finalfile = 'out.fin'
    lockfile = 'out.lock'
    def merge_chunks(self):
        try:
            with locking(open(self.lockfile, 'w'), LOCK_EX|LOCK_NB):
                with open(self.tempfile, 'w') as f:
                    print 'writing file'
                    f.write('ciao')
                    time.sleep(10)
                os.rename(self.tempfile, self.finalfile)
        except LockException:
            print 'ignoring exception'
            pass

p = Prova()
p.merge_chunks()

